package com.hongxun.design.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.hongxun.design.R;
import com.hongxun.design.activitys.StatisticalAnalysisActivity;
import com.hongxun.design.bean.ProjectSumItemBean;
import com.hongxun.design.listeners.MClickListener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zerdoor_pc on 2016/6/28.
 */
public class ProjectsumAdapter extends BaseAdapter {
    Context context;
    List<ProjectSumItemBean> itemBeans;
    LayoutInflater inflater;
    int currLength = 0;
    public int width = 1000;

    public Map<Integer, Boolean> map;

    public ProjectsumAdapter(Context context, List<ProjectSumItemBean> itemBeans, int width) {
        this.context = context;
        this.itemBeans = itemBeans;
        this.width = width;
        inflater = LayoutInflater.from(context);
        map = new HashMap<>();
        for (int i = 0; i < itemBeans.size(); i++) {
            if (i == 0) {
                map.put(i, true);
            } else {
                map.put(i, false);
            }

        }
        update();
    }

    public void update() {
        if (itemBeans == null || itemBeans.size() == 0) return;
        for (ProjectSumItemBean p : itemBeans) {
            if (!p.getName().equals(itemBeans.get(0).getName()) && Integer.parseInt(p.getValue()) > currLength) {
                currLength = Integer.parseInt(p.getValue());
            }
        }

    }

    @Override
    public int getCount() {
        return itemBeans.size();
    }

    @Override
    public Object getItem(int i) {
        return itemBeans.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ProjectSumItemBean bean = (ProjectSumItemBean) getItem(i);
        Holldler holldler = null;
        if (view == null) {
            view = inflater.inflate(R.layout.listview_item_projectsum, null);
            holldler = new Holldler(view);
            view.setTag(holldler);
        } else {
            holldler = (Holldler) view.getTag();
        }

        try {
            holldler.name.setText(bean.getName());
            holldler.value.setText(bean.getValue());
            if (bean.getViewcolor() == 0) {
                if (width != 0) {
                    holldler.progress.setVisibility(View.GONE);
                }
            } else {
                holldler.progress.setBackgroundResource(bean.getViewcolor());
            }
            if (currLength != 0 && width != 0) {
                if (i!=0){
                    holldler.progress.setVisibility(View.VISIBLE);
                }
               // holldler.progress.setVisibility(View.VISIBLE);
                ViewGroup.LayoutParams params = holldler.progress.getLayoutParams();
                float level = (width / currLength);
                params.width = (int) (Integer.parseInt(bean.getValue()) * level);
                holldler.progress.setLayoutParams(params);
            }else if (currLength==0){
                holldler.progress.setVisibility(View.GONE);
            }

            holldler.check.setOnClickListener(new MClickListener(i, bean) {
                @Override
                public void onClick(View view, int position, Object msg) {
                    ProjectSumItemBean bean = (ProjectSumItemBean) msg;

                    if (!map.get(position)){
                        for (int i = 0; i < itemBeans.size(); i++) {
                            map.put(i, false);
                        }
                        ((CheckBox) view).setChecked(true);
                        map.put(position, true);
                        ((StatisticalAnalysisActivity) context).stage = bean.getNamecode();
                        ((StatisticalAnalysisActivity) context).RefreshData();
                    }
//                    if (((CheckBox) view).isChecked()) {
//                        ((CheckBox) view).setChecked(true);
//                        map.put(position, true);
//                        ((StatisticalAnalysisActivity) context).stage = bean.getNamecode();
//                        ((StatisticalAnalysisActivity) context).RefreshData();
//                    } else {
//                        ((CheckBox) view).setChecked(false);
//                        map.put(position, false);
//                    }
                    notifyDataSetChanged();
                }
            });
            boolean res = false;
            if (map.get(i) == null || !map.get(i)) {
                res = false;
                map.put(i, false);
            } else
                res = true;

            holldler.check.setChecked(res);


        } catch (Exception e) {
        }


        return view;
    }

    public class Holldler {
        TextView name, value;
        CheckBox check;
        public View progress;

        Holldler(View v) {
            name = (TextView) v.findViewById(R.id.projectsum_name);
            value = (TextView) v.findViewById(R.id.projectsum_value);
            check = (CheckBox) v.findViewById(R.id.projectsum_check);
            progress = v.findViewById(R.id.projectsum_progress);
        }
    }
}
